package com.common.www.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class VOUtils<T> {
    public VOUtils() {
    }

    /**
     * 把数据库的列表对象转换成vo的输出列表对象以减少带宽的占用
     *
     * @param clz 需要转换成的object
     */
    public List<Object> listToVoList(List list, Class<T> clz) {
        List<Object> resList = new ArrayList<>();
        if (list == null)
            return resList;
        for (int i = 0; i < list.size(); i++) {
            try {
                T o = clz.newInstance();
                BeanUtils.copyProperties(list.get(i), o);
                resList.add(o);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return resList;
    }
}
